#!/bin/bash
#
# This script requires lcov to be installed
#

TESTS="test_media_type test_mms_codec test_delivery_ind \
test_read_ind test_read_report test_resize test_retrieve \
test_retrieve_cancel test_retrieve_no_proxy test_retrieve_order \
test_send test_settings"
FLAVOR="release"

pushd `dirname $0` > /dev/null
COV_DIR="$PWD"
pushd .. > /dev/null
TEST_DIR="$PWD"
pushd .. > /dev/null
BASE_DIR="$PWD"
popd > /dev/null
popd > /dev/null
popd > /dev/null

for t in $TESTS ; do
    make -C "$TEST_DIR/$t" clean
done

for t in $TESTS ; do
    make GCOV=1 -C "$TEST_DIR/$t" $FLAVOR || exit 1
done

for t in $TESTS ; do
    pushd "$TEST_DIR/$t"
    build/$FLAVOR/$t || exit 1
    popd
done

# LCOV 1.10 has branch coverage disabled per default
LCOV_OPT="--rc lcov_branch_coverage=1"
GENHTML_OPT="--branch-coverage"

FULL_COV="$COV_DIR/full.gcov"
MMSLIB_COV="$COV_DIR/mms-lib.gcov"
rm -f "$FULL_COV" "$MMSLIB_COV"
lcov $LCOV_OPT -c -d "$BASE_DIR/build/$FLAVOR" -b "$BASE_DIR" -o "$FULL_COV" || exit 1
lcov $LCOV_OPT -e "$FULL_COV" "$BASE_DIR/src/*" -o "$MMSLIB_COV" || exit 1
genhtml $GENHTML_OPT -t mms-engine "$MMSLIB_COV" --output-directory "$COV_DIR/results" || exit 1
